<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <!-- <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="公司名称" prop="company_name">
                        <el-input v-model="company_name" disabled placeholder="公司名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="应收金额" prop="collect_account">
                        <el-input v-model="collect_account" disabled placeholder="应收金额"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="回款金额" prop="money">
                        <el-input v-model="form.money" placeholder="回款金额"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="付款人名称" prop="pay_name">
                        <el-input v-model="form.pay_name" placeholder="付款人名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="付款时间" prop="pay_time">
                        <el-date-picker v-model="form.pay_time" type="date" placeholder="付款时间" style="width: 100%;" :value-format="'YYYY-MM-DD'"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="收款账户" prop="company_id">
                        <el-select v-model="form.company_id" placeholder="请选择收款账户" style="width:100%"  clearable>
                            <el-option v-for="item in companyList" :key="item.id" :label="item.company_name" :value="item.id"/>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="收款方式" prop="pay_type">
                        <el-select v-model="form.pay_type" placeholder="请选择收款方式" style="width:100%" clearable filterable>
                            <el-option label="公户" :value="1"/>
                            <el-option label="微信(支付宝)" :value="2"/>
                            <el-option label="银行卡" :value="3"/>
                            <el-option label="二维码" :value="4"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="上传凭证" prop="pay_files">
                        <sc-upload-file v-model="form.pay_files" :limit="9">
                            <el-button type="primary" size="small">上传文件</el-button>
                        </sc-upload-file>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="回款说明" prop="remark">
                        <el-input v-model="form.remark" placeholder="回款说明"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属部门" prop="dept_id">
                        <el-cascader v-model="form.dept_id" :options="depts" :props="deptsProps" clearable style="width: 100%;"></el-cascader>
                    </el-form-item>
                </el-col>
            </el-row> -->
            <el-form-item label="回款金额" prop="money">
                <el-input v-model="form.money" placeholder="回款金额"></el-input>
            </el-form-item>
            <el-form-item label="付款时间" prop="pay_time">
                <el-date-picker v-model="form.pay_time" type="date" placeholder="付款时间" style="width: 100%;" :value-format="'YYYY-MM-DD'"></el-date-picker>
            </el-form-item>
            <el-form-item label="收款账户" prop="company_id">
                <el-select v-model="form.company_id" placeholder="请选择收款账户" style="width:100%"  clearable>
                    <el-option v-for="item in companyList" :key="item.id" :label="item.company_name" :value="item.id"/>
                </el-select>
            </el-form-item>
            <el-form-item label="收款方式" prop="pay_type">
                <el-select v-model="form.pay_type" placeholder="请选择收款方式" style="width:100%" clearable filterable>
                    <el-option label="公户" :value="1"/>
                    <el-option label="微信(支付宝)" :value="2"/>
                    <el-option label="银行卡" :value="3"/>
                    <el-option label="二维码" :value="4"/>
                </el-select>
            </el-form-item>
            <el-form-item label="付款凭证" prop="pay_files">
                <sc-upload height="100" width="100" v-model="form.pay_files"></sc-upload>
                <!-- <sc-upload-file v-model="form.pay_files" :limit="9">
                    <el-button type="primary" size="small">上传文件</el-button>
                </sc-upload-file> -->
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增回款记录',
				edit: '新增回款记录',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id:"",
                project_id:"",
                collect_id:"",
                customer_id:"",
                money:"",
                pay_name:"",
                pay_time:"",
                company_id:"",
                pay_type:"",
                pay_files:"",
                remark:"",
                check_status:"",
                check_remark:"",
                dept_id:"",
			},
            collect_account: "",
            company_name:"",
			//验证规则
			rules: {
				project_id: [{ required: true, message: '请选择项目' }],
			},
            companyList:[],
            depts: [],
			deptsProps: {
				value: "id",
				checkStrictly: true
			},
		}
	},
	mounted() {
        this.getCompany()
        this.getDept()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.project.payment.add.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
            this.form.collect_id = data.id
            this.form.customer_id = data.project.customer_id
            this.form.project_id = data.project_id
            this.form.project_id = data.project_id
            this.company_name = data.project.customer.company_name
            this.collect_account = data.collect_account
		},
        async getCompany(){
            var res = await this.$API.finance.company.all.get();
            this.companyList = res.data;
        },
        async getDept() {
			var res = await this.$API.system.dept.list.get();
			this.depts = res.data;
		},
	}
}
</script>

<style></style>
